<?php
namespace Admin\Controller;

class FindpwdController extends \Api\Core\Web  {

	public function index() {
		$this->display();
	}


	/**
	 * 发送邮件
	 * @return [type] [description]
	 */
	public function send() {

		$this->isPost();
		$account = I ('post.account',null);
		$email = I ('post.email',null);
		$role = I ('post.role',null);

        $flag = \Api\Libs\PregUtil::preg_email($email);
        if ($flag === false) $this->out('RESULT_FAIL','邮箱格式错误');

        // 验证用户输入信息
		switch (strtolower($role)) {
            // 总管理员
            case 'super':
                $Sadmin = D ('Sadmin');
                $u = $Sadmin->checkAdminInfo($account,$email);
                break;
            // 院系管理员
            case 'department':
                $Dadmin = D ('Dadmin');
                $u = $Dadmin->checkAdminInfo($account,$email);
                break;
            // 教师
            case 'teacher':
                $Teacher = D ('Teacher');
                $u = $Teacher->checkAdminInfo($account,$email);
                break;
            default:
                $this->out('RESULT_FAIL','请选择一个相应角色才能找回密码');
        }

        if (!is_array($u))
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($u));

        // 构建表记录
        $data['rand'] = md5(rand());
        $data['account'] = $account = isset($u['account'])?$u['account']:$u['teacher_id'];
        $data['role'] = $role;
        $data['send_time'] = date('Y-m-d H:i:s');
        $flag = M('Findpwd')->add($data);
        if ($flag !== false ) {

            $title = "点名 - 找回密码";
            $url = "http://www.e8net.cn/dianm/admin.php?c=Findpwd&a=change&account={$data['account']}&role={$role}&rand={$data['rand']}";

            $html = '<div style="width:100%;">'
            . '<p>您正在使用找回密码服务，请点击下面的链接继续(有效时间30分钟)</p>'
            . '<a href="'. $url .'">'.$url.'</a>'
            . '</div>';

            $flag = think_send_mail($email,'雪晴工作室',$title,$html);
            if ($flag !== true) 
                $this->out('RESULT_FAIL','邮件发送失败');

            $type1 = explode('@', $email);
            $type2 = explode('.', $type1[1]);

            $this->out('RESULT_OK','已发送邮件,请到邮箱查阅后继续',U('Findpwd/toemail',array('type'=>$type2[0])));
        } else {
            $this->out('RESULT_FAIL','生产记录失败');
        }
	}

    
    public function toemail() {
        $type = I ('get.type',null);
        if (empty($type)) $url = '#';

        $mails = array(
                '163'=>'http://mail.163.com/',
                '126'=>'http://mail.126.com/',
                'yeah'=>'http://mail.yeah.com/',
                'qq'=>'https://mail.qq.com/cgi-bin/loginpage',
                'sina'=>'http://mail.sina.com.cn/',
                'gmail'=>'https://mail.google.com/',
                'yahoo'=>'https://login.yahoo.com/config/mail?&.src=ym&.intl=cn',
                'hotmail'=>'https://login.live.com/',
            );
        if (array_key_exists($type,$mails))
            $url = $mails[$type];

        $this->assign('url',$url);
        $this->display();
    }


    /**
     * 修改密码
     * @return [type] [description]
     */
    public function change() {

        $account = I ('get.account',null);
        $role = I ('get.role',null);
        $rand = I ('get.rand',null);

        if (empty($account) || empty($role) || empty($rand))
        {
            $this->display('error-page');
            die();
        }

        $Model = D ('Findpwd');
        $result = $Model->checkInfo($role,$account,$rand);

        if ($result === false) 
        {
            $this->display('error-page');
            die();
        }

        $this->assign('account',$account);
        $this->assign('role',$role);
        $this->display();

    }

    public function edit() {
        $this->isPost();

        $account = I ('post.account',null);
        $role = I ('post.role',null);
        $pwd = I ('post.pwd',null);

        if (empty($account) || empty($role) || empty($pwd)) 
            $this->out('RESULT_FAIL','错误的参数');

        $map['account'] = $account;
        switch (strtolower($role)) {
            case 'super':
                $role = 'Sadmin';  
                break;
            case 'department':
                $role = 'Dadmin';
                break;
            case 'teacher':
                $role = 'Teacher';
                unset($map);
                $map['teacher_id'] = $account;
                break;
            default:
                $this->out('RESULT_FAIL','无法识别的角色');
                die();
        }

        $Model = M ($role);
        $flag = $Model->where($map)->setField('password',md5($pwd));
        if ($flag === false)
            $this->out('RESULT_FAIL','修改密码失败');
        $this->out('RESULT_OK','',U('Findpwd/ok'));

    }

    public function ok() {
        $this->display('success-page');
    }

}